package com.ruibang.glass.etl.listener;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.gitee.Jmysy.binlog4j.core.BinlogEvent;
import com.ruibang.glass.etl.entity.BcADefectData;
import com.ruibang.glass.etl.mapper.BcDefectDataMapper;
import com.teaming.cloud.framework2.common.tools.JsonUtils;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedHashMap;

import static com.ruibang.glass.etl.constant.RocketMQConstants.DEFECT_TOPIC;
import static com.ruibang.glass.etl.constant.RocketMQConstants.DEFECT_GROUP;

//@RocketMQMessageListener(consumerGroup = DEFECT_GROUP,
//        topic = DEFECT_TOPIC)
//@Service
public class DefectDataRocketMQListener implements RocketMQListener<String> {

    @Autowired
    BcDefectDataMapper bcDefectDataMapper;

    @Override
    public void onMessage(String s) {
        BinlogEvent<LinkedHashMap> event = JsonUtils.parseObject(s, BinlogEvent.class);
        if(null != event && null != event.getData()){
            LinkedHashMap bcADefectData = event.getData();
            System.out.println("DefectDataRocketMQListener:" + bcADefectData);
            UpdateWrapper<BcADefectData> updateWrapper = new UpdateWrapper<>();
            updateWrapper.set("TiltedSize", bcADefectData.get("tiltedSize"))
                    .set("TiltedDepth",bcADefectData.get("f1"))
                    .set("TiltedDiv",bcADefectData.get("f2"))
                    .eq("DefectId",bcADefectData.get("defectId"))
                    .eq("sourceDB", event.getDatabase());
            int rows = bcDefectDataMapper.update(null, updateWrapper);
            System.out.println("bcDefectDataMapper rows:" + rows);
        }
    }
}